/*
*
* FILENAME:		linear_app.c/linear_app.h 
* CREATED AT:		2012/May/5
* DECRITION:		the module lineaa list, include sequence implement and list implement
* 
* MODULE:		linead list
* PLATFORM:		ubuntu-12.04/gcc-4.6.3
* AUTHOR:		incarnation-lee
* 
* ----------------------REVISION HISTROY------------------------
* No.	Date	Revised by	Function	Descrition
* 1	12/5/7	incar-lee	linearlist_demo	created the linear list demo function
*
*/

/* Standard C */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

/* linux related */
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

/* user defined */
#include "externals.h"
#include "udgtd.h"
#include "linear_app.h"
#include "errcode.h"


STATUS linear_app_demo(void)
{
	STATUS result = 0;
	/* sequence linear implement */
	result = linear_seq_demo();
	if(__builtin_expect(!!result,0))
		return -1;

	/* single linker linear implement */
	result = linear_slk_demo();
	if(__builtin_expect(!!result,0))
		return -1;
	return 0;
}


STATUS linear_seq_demo(void)
{
	S_sqlinear lseq_demo;
	STATUS result = 0;
	S_element element = {
		.value = 97,
		.number = 1,
		.name = 'a',
	},*ele_linear,*ele_next,*ele_prior;

	/* initiliazie the linear sequence list */
	result = init_lseq(&lseq_demo);
	if(__builtin_expect(!!(result),0))
		return -1;	

	/* add 3 ASCII element in linear list */
	result = add_lseq(&lseq_demo,&element);
	if(__builtin_expect(!!result,0))
		return -1;
	
	element.value = 98;
	element.number = 2;
	element.name = 'b';
	result = add_lseq(&lseq_demo,&element);
	if(__builtin_expect(!!result,0))
		return -1;

	element.value = 99;
	element.number = 3;
	element.name = 'c';
	result = add_lseq(&lseq_demo,&element);
	if(__builtin_expect(!!result,0))
		return -1;

	/* get the address in linear list element */
	ele_linear = get_lseq(&lseq_demo,1);
	if(__builtin_expect(!!(NULL==ele_linear),0))
		return -1;

	/* get the next element of specify index */
	ele_next = next_lseq(&lseq_demo,1);	
	if(__builtin_expect(!!(NULL==ele_next),0))
		return -1;

	/* get the next element of specify index */
	ele_prior = prior_lseq(&lseq_demo,1);	
	if(__builtin_expect(!!(NULL==ele_prior),0))
		return -1;

	/* insert a new element */
	element.value = 100;
	element.number = 4;
	element.name = 'd';
	result = insert_lseq(&lseq_demo,&element,1);
	if(__builtin_expect(!!result,0))
		return -1;

	/* delete the insert element */
	result = delete_lseq(&lseq_demo,1);
	if(__builtin_expect(!!result,0))
		return -1;

	/* is the linear list empty */
	result = isEmpty_lseq(&lseq_demo);
	if(-1==result)
		return -1;
/* here inseart your operation code on if empty */
	
	/* is the linear list empty */
	result = len_lseq(&lseq_demo);
	if(NoEXIT==result)
		return -1;
/* here inseart your operation code on the length exit */

	/* clear the linear list, and reset the rest field */		
	result = clear_lseq(&lseq_demo);
	if(__builtin_expect(!!(result),0))
		return -1;

	/* destory the whole linear list */
	result = destory_lseq(&lseq_demo);
	if(__builtin_expect(!!(result),0))
		return -1;

	return 0;
}


STATUS linear_slk_demo(void)
{
	int result = 0;
	S_slk_lr* slk_head_demo = NULL;
	S_slk_lr* slk_sp;
	S_slk_lr slk_e_1,slk_e_2,slk_e_3,slk_e_4,slk_e_ins;


	/* initilize the single linker linear */
	result = init_slnk(&slk_head_demo);	
	if(__builtin_expect(!!(result),0))
		return -1;
	
	/* clear all the element of linear list */
	result = clear_slnk(&slk_head_demo);

	/* add a new element append the single linker linear list */
	gmalloc((void**)&slk_e_1.elem,sizeof(S_element));
	slk_e_1.next = NULL;
	slk_e_1.elem->value = 97;
	slk_e_1.elem->number = 1;
	slk_e_1.elem->name = 'a';
	result = add_slnk(slk_head_demo,&slk_e_1);

	
	gmalloc((void**)&slk_e_2.elem,sizeof(S_element));
	slk_e_2.next = NULL;
	slk_e_2.elem->value = 98;
	slk_e_2.elem->number = 2;
	slk_e_2.elem->name = 'b';
	result = add_slnk(slk_head_demo,&slk_e_2);
	
	gmalloc((void**)&slk_e_3.elem,sizeof(S_element));
	slk_e_3.next = NULL;
	slk_e_3.elem->value = 99;
	slk_e_3.elem->number = 3;
	slk_e_3.elem->name = 'c';
	result = add_slnk(slk_head_demo,&slk_e_3);

	/* get the element of specify element in linear list */
	slk_sp = get_slnk(slk_head_demo,&slk_e_2);	

	gmalloc((void**)&slk_e_4.elem,sizeof(S_element));
	slk_e_4.next = NULL;
	slk_e_4.elem->value = 100;
	slk_e_4.elem->number = 4;
	slk_e_4.elem->name = 'd';
	slk_sp = get_slnk(slk_head_demo,&slk_e_4);	
	assert(NULL==slk_sp);
	
	
	/* the next element */
	slk_sp = next_slnk(slk_head_demo,&slk_e_1);
	slk_sp = next_slnk(slk_head_demo,&slk_e_2);
	slk_sp = next_slnk(slk_head_demo,&slk_e_4);

	/* the prior element*/
	slk_sp = prior_slnk(slk_head_demo,&slk_e_1);
	slk_sp = prior_slnk(slk_head_demo,&slk_e_2);
	slk_sp = prior_slnk(slk_head_demo,&slk_e_4);

	/* insert a data element */
	gmalloc((void**)&slk_e_ins.elem,sizeof(S_element));
	slk_e_ins.next = NULL;
	slk_e_ins.elem->value = 101;
	slk_e_ins.elem->number = 5;
	slk_e_ins.elem->name = 'e';
	result = insert_slnk(slk_head_demo,&slk_e_ins,&slk_e_1);

	/* delete a data element */
	result = delete_slnk(slk_head_demo,&slk_e_1);

	/* linear list is empty */
	result = isEmpty_slnk(slk_head_demo);

	/* the number of linear list */
	result = len_slnk(slk_head_demo);

#if 1  	/* enabled this test the other branch of destory function */
	result = clear_slnk(&slk_head_demo);
#endif

	/* destory single linker linear */
	result = destory_slnk(&slk_head_demo);

	return result;
}
